package com.example.myuniversity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.university.utils.HotUniversity;
import com.university.utils.SqliteDatabaseManager;

public class HotUniversityActivity extends Activity{
	private ImageView imageFinish;
	private ExpandableListView listView;
	private BaseExpandableListAdapter adapter;
	private Cursor cursor;
	private Map<String, ArrayList<HotUniversity>> eMap;
	private SQLiteDatabase mDatabase;
	private String[] groupNames={"综合类高校报考热度榜","理工类高校报考热度榜","师范类高校报考热度榜","财经类高校报考热度榜","政法类高校报考热度榜","外语类高校报考热度榜"
			,"医科类高校报考热度榜","农林类高校报考热度榜","民族类高校报考热度榜","艺术类高校报考热度榜","体育类高校报考热度榜","民办高校报考热度榜","独立学院报考热度榜","高职高专报考热度榜"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hotuniversity);
		init();
		setOnClick();
		mDatabase=SqliteDatabaseManager.getInstance(this).getDatabase();
		eMap=new HashMap<String, ArrayList<HotUniversity>>();
		cursor=mDatabase.query("热门大学表", new String[]{"榜名","排名","院校名称" }, null, null, null, null, null);
		int i=0;
		ArrayList<HotUniversity> arrayList=new ArrayList<HotUniversity>();
		while (cursor.moveToNext()) {
			i++;
			HotUniversity university=new HotUniversity(cursor.getString(0),cursor.getInt(1),cursor.getString(2));
			arrayList.add(university);
			if (i%10==0) {
				eMap.put(university.getuList(), arrayList);
				arrayList=new ArrayList<HotUniversity>();
				i=0;
			}
		}
		cursor.close();
		adapter=new MyAdapater();
		listView.setAdapter(adapter);
	}
	private void init() {
		imageFinish=(ImageView) findViewById(R.id.image_hotuniversity_finish);
		listView=(ExpandableListView) findViewById(R.id.expandable_listview_hotuniversity);
	}
	
	private void setOnClick() {
		imageFinish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		listView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				String schoolId="空";
				String schoolName=eMap.get(groupNames[groupPosition]).get(childPosition).getuName();
				cursor=mDatabase.query("院校表", new String[]{"院校代码"}, "院校名称=?", new String[]{schoolName}, "院校代码", null, null);
				while (cursor.moveToNext()) {
					schoolId=cursor.getString(0);
				}
				Intent intent=new Intent(HotUniversityActivity.this, MajorDetailsActivity.class);
				intent.putExtra("SchoolName", schoolName);
				intent.putExtra("SchoolId", schoolId);
				startActivity(intent);
				return false;
			}
		});
	}
	
	class MyAdapater extends BaseExpandableListAdapter{
		private TextView txtName;
		@Override
		public int getGroupCount() {
			return groupNames.length;
		}
		@Override
		public int getChildrenCount(int groupPosition) {
			return eMap.get(groupNames[groupPosition]).size();
		}
		@Override
		public Object getGroup(int groupPosition) {
			return groupNames[groupPosition];
		}
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return eMap.get(groupNames[groupPosition]);
		}
		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
		@Override
		public boolean hasStableIds() {
			return false;
		}
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			if (convertView==null) {
				convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_hotuniversity_group, parent, false);
			}
				txtName=(TextView) convertView.findViewById(R.id.txt_expandable_group_title);
				txtName.setText(groupNames[groupPosition]);
			return convertView;
		}
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			
			ViewHolder viewHolder=null;
			if (convertView==null) {
				convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_hotuniversity_children, parent,false);
				viewHolder=new ViewHolder();
				viewHolder.txtNumber=(TextView) convertView.findViewById(R.id.txt_expandable_children_number);
				viewHolder.txtName=(TextView) convertView.findViewById(R.id.txt_expandable_children_title);
				convertView.setTag(viewHolder);
			}else {
				viewHolder=(ViewHolder) convertView.getTag();
			}
				viewHolder.txtName.setText(eMap.get(groupNames[groupPosition]).get(childPosition).getuName());
				viewHolder.txtNumber.setText(eMap.get(groupNames[groupPosition]).get(childPosition).getuNumber()+"");
			return convertView;
		}
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
	}
	private static class ViewHolder{
		TextView txtNumber,txtName;
	}
	@Override
	protected void onDestroy() {
		if (cursor!=null) {
			cursor.close();
		}
		super.onDestroy();
	}
	
}
